/****************************************************
	文件：AIBase.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/8/27 13:58:31
	功能：AI基类（框架层）
*****************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;

namespace AIFramework.HTN
{
    public class AIBase
    {
        protected EntityBase mSelf;
        protected DomainBase mDomain;
        protected Planner mPlanner;
        protected PlanRunner mRunner;
        protected EntityBase mOpponent;
        protected string mDest;
        protected string mCurAnimName;

        /// <summary>
        /// 对手
        /// </summary>
        public EntityBase Opponent { get => mOpponent; }
        /// <summary>
        /// 目标地
        /// </summary>
        public string Dest { get => mDest; }
        /// <summary>
        /// 当前播放的动画名称
        /// </summary>
        public string CurAnimName { get => mCurAnimName; }

        public AIBase(EntityBase self)
        {
            mSelf = self;
        }
        public virtual void Reset()
        {
            mSelf = null;
            mDomain = null;
        }
        public void Init()
        {
            mDomain.Build();
            mPlanner = new Planner();
            mPlanner.Init(mSelf.CurrentWorldState, mDomain);
            mRunner = new PlanRunner(mSelf);
        }

        /// <summary>
        /// 开始计划
        /// </summary>
        public void DoPlan()
        {
            mPlanner.BulidPlan();
        }
        /// <summary>
        /// 执行计划
        /// </summary>
        public void DoRun()
        {
            mRunner.Run(mPlanner.GetFinalTasks());
        }
        /// <summary>
        /// 设置对手
        /// </summary>
        /// <param name="opponent"></param>
        public void SetOpponent(EntityBase opponent)
        {
            mOpponent = opponent;
        }
        /// <summary>
        /// 设置目标地
        /// </summary>
        /// <param name="dest">目标地</param>
        public void SetDest(string dest)
        {
            mDest = dest;
        }
        /// <summary>
        /// 设置当前播放的动画名称
        /// </summary>
        /// <param name="animName">动画名称</param>
        public void SetAnim(string animName)
        {
            mCurAnimName = animName;
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="task">任务对象</param>
        /// <returns></returns>
        public AIBase AddTask(TaskBase task)
        {
            mDomain.AddTask(task);
            return this;
        }
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="task">任务对象</param>
        /// <returns></returns>
        public bool RemoveTask(TaskBase task)
        {
            return RemoveTask(task.Name);
        }
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="taskName">任务名称</param>
        /// <returns></returns>
        public bool RemoveTask(string taskName)
        {
            return mDomain.RemoveTask(taskName);
        }
        /// <summary>
        /// 搜索任务
        /// </summary>
        /// <param name="taskName">任务名称</param>
        /// <returns></returns>
        public TaskBase FindTask(string taskName)
        {
            return mDomain.FindTask(taskName);
        }
    }
}
